<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <!-- 查询条件 -->
      <el-form :model="searchForm" label-width="100px" class="search-form">
        <el-row>
          <el-col :span="8">
            <el-form-item label="单位编号">
              <unit-rpc ref="unitRpc" @unitRpcCallBack="unitRpcCallBack" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="单位名称">
              <el-input v-model="searchForm.aab004" placeholder="请输入单位名称" :disabled="true" />
            </el-form-item>
          </el-col>
          <el-col :span="8" class="text-right" style="float: right">
            <el-form-item>
              <el-button type="primary" @click="search">查询</el-button>
              <el-button @click="reset">重置</el-button>
            </el-form-item>
          </el-col>
          <!--            <el-col :span="8">-->
          <!--              <el-form-item label="经办时间">-->
          <!--                <el-date-picker-->
          <!--                  v-model="searchForm.startDate"-->
          <!--                  type="daterange"-->
          <!--                  start-placeholder="开始日期"-->
          <!--                  end-placeholder="结束日期"-->
          <!--                  :picker-options="pickerOptions"-->
          <!--                  style="width: 100%"-->
          <!--                ></el-date-picker>-->
          <!--              </el-form-item>-->
          <!--            </el-col>-->
        </el-row>
      </el-form>

      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="1000px">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
          <el-row :gutter="24">
            <el-col :span="8">
              <el-form-item label="单位名称" prop="aab004">
                <el-input v-model="form.aab004" />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="统一社会信用代码">
                <el-input v-model="form.aab998" />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <!--          <el-form-item label="所属行业">-->
              <!--            未设置字典，请手动设置 Select-->
              <!--          </el-form-item>-->
              <el-form-item label="行业风险等级" prop="aaa149">
                <el-select v-model="form.aaa149" placeholder="请选择">
                  <el-option v-for="item in dict.aaa149" :key="item.id" :value="item.value" :label="item.label" />
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="24">
            <el-col :span="8">
              <el-form-item label="单位类型" prop="aab019">
                <el-select v-model="form.aab019" placeholder="请选择">
                  <el-option v-for="item in dict.aab019" :key="item.id" :value="item.value" :label="item.label" />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="法人姓名">
                <el-input v-model="form.aab013" />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="法人联系电话">
                <el-input v-model="form.aae005" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="24">
            <el-col :span="8">
              <el-form-item label="法人证件号码">
                <el-input v-model="form.aac147" />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="单位联系人">
                <el-input v-model="form.bab009" />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="单位联系电话">
                <el-input v-model="form.bab012" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="24">
            <el-col :span="12">
              <el-form-item label="单位地址">
                <el-input v-model="form.aab361" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="备注">
                <el-input v-model="form.aae013" />
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler" @row-dblclick="handleRowDblClick">
        <el-table-column type="selection" width="55" />
        <el-table-column prop="aab001" label="单位编号" width="150px" fixed="left" />
        <el-table-column prop="aab004" label="单位名称" width="200px" fixed="left" />
        <el-table-column prop="aab998" label="统一社会信用代码" width="150px" />
        <!--        <el-table-column prop="aab022" label="所属行业" />-->
        <el-table-column prop="aaa149" label="行业风险等级" width="150px" :formatter="formatter" />
        <el-table-column prop="aab019" label="单位类型" :formatter="formatter" />
        <el-table-column prop="bab009" label="单位联系人" />
        <el-table-column prop="bab012" label="单位联系电话" width="150px" />
        <el-table-column prop="aab361" label="单位注册地" />
        <el-table-column prop="aab013" label="法人姓名" />
        <el-table-column prop="aae005" label="法人联系电话" width="150px" />
        <el-table-column prop="aac147" label="法人证件号码" width="150px" />
        <el-table-column prop="aae013" label="备注" />
        <el-table-column v-if="checkPer(['admin','basicAb01:edit','basicAb01:del'])" label="操作" width="150px" align="center" fixed="right">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
      <!-- 弹窗 -->
      <el-dialog :visible.sync="dialogVisible" title="单位参保信息" :style="{ zIndex: 1000 }">
        <!--      // 注入·参保信息-->
        <ab02 ref="ab02ref" :permission="permission" :aab001="aab001" @queryCallBack="queryCallBack" />
        <el-button @click="dialogVisible = false">关闭</el-button>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import crudBasicAb01 from '@/views/ynsb_comm/ab01/basicAb01'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import ab02 from '../ab02/index'
import UnitRpc from '@/views/ynsb_comm/comm/module/unitRpc'

const defaultForm = { aab361: null, bab009: null, bab012: null, aab001: null, aab004: null, aab998: null, aab022: null, aaa149: null, aab019: null, aab013: null, aae005: null, aac147: null, aae013: null }
export default {
  name: 'BasicAb01',
  components: { pagination, crudOperation, udOperation, ab02, UnitRpc },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({ title: '单位基础信息采集', url: 'api/basicAb01', idField: 'aab001', sort: 'aab001,desc', crudMethod: { ...crudBasicAb01 }})
  },
  // 数据字典
  dicts: ['aaa149', 'aab019'],
  data() {
    return {
      searchForm: {
        aab001: '', // 单位编号
        aab004: '' // 单位名称
      },
      permission: {
        add: ['admin', 'basicAb01:add'],
        edit: ['admin', 'basicAb01:edit'],
        del: ['admin', 'basicAb01:del']
      },
      rules: {
        aab001: [
          { required: true, message: '单位编号不能为空', trigger: 'blur' }
        ],
        aab004: [
          { required: true, message: '单位名称不能为空', trigger: 'blur' }
        ]
      },
      dialogVisible: false, // 控制弹窗的显示与隐藏
      selectedRow: null, // 存储被双击的行数据
      aab001: '' // 单位编号
    }
  },
  methods: {
    formatter(row, column, cellValue) {
      // 根据 aae140 的值进行转码
      const key = column.property
      const data = this.dict[key]
      const dataList = data.filter(item => item.value === cellValue)
      console.log(dataList, 'data')
      if (dataList !== null && dataList !== [] && dataList !== undefined && dataList.length > 0) {
        return dataList[0].label
      } else {
        return cellValue
      }
    },
    search() {
      // 假设查询的数据
      crudBasicAb01.getAb01({ ...this.searchForm }).then(res => {
        console.log(res, 'res')
        this.crud.data = res.content
      })
    },
    reset() {
      this.$refs.unitRpc.restAab001('')
      this.searchForm = {
        aab001: '',
        aab004: ''
      }
      this.tableData = [] // 清空表格数据
    },
    unitRpcCallBack(item) {
      this.form.aab001 = item.aab001
      this.searchForm = {
        aab001: item.aab001,
        aab004: item.aab004
      }
    },
    // 双击行触发的事件
    handleRowDblClick(row) {
      this.aab001 = row.aab001
      this.selectedRow = row // 获取双击的行数据
      this.dialogVisible = true // 显示弹窗
      setTimeout(() => {
        this.$refs.ab02ref.getAb02s()
      }, 500) // 延迟1秒后显示弹窗
    },
    queryCallBack() {
      this.dialogVisible = false // 显示弹窗
    },
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    }
  }
}
</script>

<style scoped>

</style>
